#include "mytcpserver.h"
#include "mytcpsocket.h"


MyTcpServer::MyTcpServer()
{

}

MyTcpServer &MyTcpServer::getInstance()
{
    static MyTcpServer instance;
    return instance;
}

//当客户端连接到来时，会自动调用该函数
void MyTcpServer::incomingConnection(qintptr socketDescriptor)
{
    qDebug()<<"new client connected";
    MyTcpSocket* socket = new MyTcpSocket();
    socket->setSocketDescriptor(socketDescriptor);
    m_tcpSocketList.append(socket);
    connect(socket,SIGNAL(offline(MyTcpSocket*)),this,SLOT(deleteSocket(MyTcpSocket*)));
}

void MyTcpServer::deleteSocket(MyTcpSocket *mysocket)
{
    QList<MyTcpSocket*>::iterator iter = m_tcpSocketList.begin();
    for(;iter!=m_tcpSocketList.end();iter++)
    {
        if(mysocket==*iter)
        {
            (*iter)->deleteLater();
            *iter =NULL;
            m_tcpSocketList.erase(iter);
            break;
        }
    }

}
